package com.seeTools.boot.pc.service.sys.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seeTools.boot.api.entity.sys.SysDictData;
import com.seeTools.boot.api.entity.sys.SysDictType;
import com.seeTools.boot.common.constant.Constants;
import com.seeTools.boot.common.core.PageQuery;
import com.seeTools.boot.common.tools.StreamUtils;
import com.seeTools.boot.pc.mapper.sys.SysDictDataMapper;
import com.seeTools.boot.pc.service.BaseServiceImpl;
import com.seeTools.boot.pc.service.sys.SysDictDataService;
import com.seeTools.boot.pc.service.sys.SysDictTypeService;
import com.seeTools.boot.pc.util.PageQueryHelper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: seeTools
 * @Date: 2024/6/7  14:37
 * @Version 1.0
 */
@Service
public class SysDictDataServiceImpl extends BaseServiceImpl<SysDictDataMapper, SysDictData> implements SysDictDataService {


    @Resource
    private PageQueryHelper<SysDictData> pageQueryHelper;
    @Lazy
    @Resource
    private SysDictTypeService sysDictTypeService;

    /**
     * 分页查询
     *
     * @param pageQuery 分页数据
     * @return
     */
    @Override
    public Page<SysDictData> findList(PageQuery<SysDictData> pageQuery) {
        QueryWrapper<SysDictData> queryWrapper = getQueryWrapper(pageQuery.getDataObj());
        return pageQueryHelper.pageInfo(this, pageQuery, queryWrapper);
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        LambdaQueryWrapper<SysDictData> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDictData::getStatus, Constants.DictType.DICT_NORMAL)
                .eq(SysDictData::getDictType, dictType)
                .orderByAsc(SysDictData::getDictSort);
        List<SysDictData> list = list(lambdaQueryWrapper);
        if (CollUtil.isNotEmpty(list)) {
            return list;
        }
        return null;
    }


    /**
     * 获取详情
     *
     * @param dictCode
     * @return
     */
    @Override
    public SysDictData getInfo(Long dictCode) {
        SysDictData sysDictData = getById(dictCode);
        SysDictType sysDictType = sysDictTypeService.findOne(SysDictType::getDictType, sysDictData.getDictType());
        sysDictData.setDictName(sysDictType.getDictName());
        return sysDictData;
    }


    /**
     * 根据条件分页查询字典数据
     *
     * @param sysDictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataList(SysDictData sysDictData) {
        QueryWrapper<SysDictData> queryWrapper = getQueryWrapper(sysDictData);
        return list(queryWrapper);
    }

    /**
     * 根据字典类型和字典值获取字典标签
     *
     * @param dictType
     * @param dictValue
     * @return
     */
    @Override
    public String getDictLabel(String dictType, String dictValue) {
        LambdaQueryWrapper<SysDictData> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDictData::getDictType, dictType).eq(SysDictData::getDictValue, dictValue);
        SysDictData sysDictData = getOne(lambdaQueryWrapper);
        return sysDictData.getDictLabel();
    }


    /**
     * 根据字典类型和字典值获取字典标签s
     *
     * @param dictType
     * @param dictValues
     * @return
     */
    @Override
    public String getDictLabels(String dictType, String dictValues) {
        List<Long> list = Convert.toList(Long.class, dictValues.split(","));
        LambdaQueryWrapper<SysDictData> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDictData::getDictType, dictType).in(SysDictData::getDictValue, list);
        List<SysDictData> dictDataList = list(lambdaQueryWrapper);
        if (CollUtil.isNotEmpty(dictDataList)) {
            List<String> collect = dictDataList.stream().map(SysDictData::getDictLabel).collect(Collectors.toList());
            return CollUtil.join(collect, ",");
        }
        return null;
    }


    /**
     * 根据字典类型和字典标签获取字典值
     *
     * @param dictType  字典类型
     * @param dictLabel 字典标签
     * @return 字典值
     */
    @Override
    public String getDictValue(String dictType, String dictLabel) {
        LambdaQueryWrapper<SysDictData> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDictData::getDictType, dictType).eq(SysDictData::getDictLabel, dictLabel);
        SysDictData sysDictData = getOne(lambdaQueryWrapper);
        return sysDictData.getDictLabel();
    }


    /**
     * 根据字典类型和字典标签获取字典值
     *
     * @param dictType   字典类型
     * @param dictLabels 字典标签
     * @return 字典值
     */
    @Override
    public String getDictValues(String dictType, String dictLabels) {
        List<Long> list = Convert.toList(Long.class, dictLabels.split(","));
        LambdaQueryWrapper<SysDictData> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDictData::getDictType, dictType).in(SysDictData::getDictLabel, list);
        List<SysDictData> dictDataList = list(lambdaQueryWrapper);

        if (CollUtil.isNotEmpty(dictDataList)) {
            List<String> collect = dictDataList.stream().map(SysDictData::getDictLabel).collect(Collectors.toList());
            return CollUtil.join(collect, ",");
        }
        return null;
    }

    /**
     * 获取字典下所有的字典值与标签
     *
     * @param dictType 字典类型
     * @return dictValue为key，dictLabel为值组成的Map
     */
    @Override
    public Map<String, String> getAllDictByDictType(String dictType) {
        List<SysDictData> list = findBy(SysDictData::getDictType, dictType);
        return StreamUtils.toMap(list, SysDictData::getDictValue, SysDictData::getDictLabel);
    }

    /**
     * 获取查询条件
     *
     * @param sysDictData
     * @return
     */
    public QueryWrapper<SysDictData> getQueryWrapper(SysDictData sysDictData) {
        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<SysDictData> lambdaQueryWrapper = queryWrapper.lambda();
        // 字典ID
        if (ObjectUtil.isNotEmpty(sysDictData.getDictId())) {
            lambdaQueryWrapper.eq(SysDictData::getDictId, sysDictData.getDictId());
        }
        // 字典类型
        if (StrUtil.isNotBlank(sysDictData.getDictType())) {
            lambdaQueryWrapper.eq(SysDictData::getDictType, sysDictData.getDictType());
        }
        // 字典标签
        if (StrUtil.isNotBlank(sysDictData.getDictLabel())) {
            lambdaQueryWrapper.like(SysDictData::getDictLabel, sysDictData.getDictLabel());
        }
        // 状态
        if (StrUtil.isNotBlank(sysDictData.getStatus())) {
            lambdaQueryWrapper.eq(SysDictData::getStatus, sysDictData.getStatus());
        }
        return queryWrapper;
    }
}
